/**
 * ClientUIController.java
 * Created on 28-Mar-2011, 12:16:18 PM
 */
package pos.desktop.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import pos.entity.Customer;
import pos.interfaces.Database;
import pos.storage.StorageFactory;
import pos.storage.db.sql.SQL;

/**
 * @since 30 MARCH 2011, 5:00 PM
 * @author Anup
 * <b>Description:<b> <br>This Class is used as a controller for the customer
 * login on applet use case. It receives the information from the user and
 * validates the data against the data in the database.
 */
public class CustomerLoginControl {

    /**
     * @date 28-Mar-2011, 12:16:18 PM
     * @param customer 
     */
    public static void validateCustomer(Customer customer) {
        //get the database connection and query
        try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement(SQL.VALIDATE_CUSTOMER);
            ps.setString(1, customer.getID().toString());
            ps.setString(2, customer.getPassword());
            
            //Reading the customer information from the database.
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                customer.setName(rs.getString("CustomerFirstName"));
                customer.setLastName(rs.getString("CustomerLastName"));
                customer.setUnitNumber(rs.getString("CustomerUnitNumber"));
                customer.setAddress(rs.getString("CustomerAddress"));
                customer.setCity(rs.getString("CustomerCity"));
                customer.setCountry(rs.getString("CustomerCountry"));
                customer.setPhoneNumber(rs.getString("CustomerPhoneNumber"));
                customer.setPostalCode(rs.getString("CustomerPostalCode"));
                customer.setEmail(rs.getString("CustomerEmail"));
                customer.setAuthenticated(true);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        
    }
    
}
